<#include "/macro.include"/>
<#include "/custom.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first> 
package ${rootPackage}.boot.core.service.impl;

import javax.annotation.Resource;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import com.github.wxiaoqi.security.checkout.common.utils.convert.GenericConvert;
import com.github.wxiaoqi.security.checkout.common.utils.lang.Paginator;
import com.github.wxiaoqi.security.checkout.common.utils.util.PageList;
import com.github.wxiaoqi.security.checkout.common.utils.CommonUtils;
import ${basePackage}.daointerface.${className}DAO;
import ${basePackage}.dataobject.${className}DO;
import com.github.wxiaoqi.security.checkout.model.${className}Model;
import com.github.wxiaoqi.security.checkout.service.${className}Service;

/**
* 类名: ${className}ServiceImpl
* @author wjt
*/
@Service("${classNameLower}Service")
public class ${className}ServiceImpl implements ${className}Service {

    @Resource
    private ${className}DAO ${classNameLower}DAO;
	
    @Override
    public ${className}Model findById(<#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long id</#if><#elseif column.javaType == "java.lang.Integer">Integer id<#else></#if></#list>) {
        ${className}DO ${classNameLower}DO = ${classNameLower}DAO.findById(id);
        ${className}Model ${classNameLower}Model = new ${className}Model();
        if(${classNameLower}DO != null) BeanUtils.copyProperties(${classNameLower}DO, ${classNameLower}Model);
        return ${classNameLower}Model;
    }

    @Override
    public Map save(${className}Model ${classNameLower}Model) {
        // 处理结果
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);

        // 参数转换
        ${className}DO ${classNameLower}DO = new ${className}DO();
        BeanUtils.copyProperties(${classNameLower}Model, ${classNameLower}DO);

        int i;
        <#list table.columns as column>
            <#if column.pk>
        // 修改
        if (${classNameLower}DO.get${column.columnNameLower?cap_first}() != null && ${classNameLower}DO.get${column.columnNameLower?cap_first}() != 0) {
            ${classNameLower}DO.setModifyTime(new Date());
            i = ${classNameLower}DAO.update(${classNameLower}DO);
            result.put("data", i);
            return result;
        }

        // 设置默认参数
        ${classNameLower}DO.setIsFlag("0");      // 状态：0-有效
        ${classNameLower}DO.setDelFlag("0");     // 是否删除: 0-有效,1-删除
        ${classNameLower}DO.setCreateTime(new Date());

        // 新增
        i = ${classNameLower}DAO.add(${classNameLower}DO);
            </#if>
        </#list>
        result.put("data", i);
        return result;
    }

    @Override
    public int delete(<#list table.columns as column><#if column.pk><#if column.javaType == "java.lang.Long">Long id</#if><#elseif column.javaType == "java.lang.Integer">Integer id<#else></#if></#list>) {
        ${className}DO ${classNameLower}DO = new ${className}DO();
        ${classNameLower}DO.set<#list table.columns as column><#if column.pk>${column.columnName}</#if></#list>(id);
        ${classNameLower}DO.setDelFlag("1");
        ${classNameLower}DO.setDelTime(new Date());
        return ${classNameLower}DAO.update(${classNameLower}DO);
    }

    @Override
    public List<${className}Model> listByParams(${className}Model ${className?uncap_first}Model) {
        Map<String,Object> params = new HashMap<String,Object>();

        // 参数转换
        ${className}DO ${classNameLower}DO = new ${className}DO();
        BeanUtils.copyProperties(${classNameLower}Model, ${classNameLower}DO);
        params = CommonUtils.getStringObjectMap(${className?uncap_first}DO, params);

        List<${className}DO> ${classNameLower}DOList = ${classNameLower}DAO.findListByParams(params);
        List<${className}Model> ${classNameLower}List = null;
        if(${classNameLower}DOList != null && ${classNameLower}DOList.size() > 0){
            ${classNameLower}List = GenericConvert.convert(${classNameLower}DOList, ${className}Model.class);
        }
        return ${classNameLower}List;
    }

    @Override
    public PageList<${className}Model> pageListByParams(${className}Model ${className?uncap_first}Model, int pageIndex, int pageSize) {

        // 参数转换
        ${className}DO ${classNameLower}DO = new ${className}DO();
        BeanUtils.copyProperties(${classNameLower}Model, ${classNameLower}DO);
        Map<String,Object> params = new HashMap<String,Object>();
        params = CommonUtils.getStringObjectMap(${className?uncap_first}DO, params);

        PageList<${className}Model> pageList = new PageList<${className}Model>();
        int count = ${classNameLower}DAO.count(params);
        if (count > 0) {
            Paginator paginator = new Paginator(pageSize, count);
            paginator.setPage(pageIndex);
            params.put("beginIndex", paginator.getOffset());
            params.put("pageSize", pageSize);
            pageList.setPaginator(paginator);

            // List DO 转 Model
            List<${className}DO> list = ${classNameLower}DAO.findPageListByParams(params);
            List<${className}Model> models = new ArrayList<${className}Model>();
            for (${className}DO obj : list) {
               ${className}Model model = new ${className}Model();
               BeanUtils.copyProperties(obj, model);
               models.add(model);
            }
            pageList.addAll(models);
        }

        return pageList;
   }

   <#--/** 对象转map*/
   private Map<String, Object> getStringObjectMap(Object obj, Map<String, Object> params) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            // 对象转json
            String json = mapper.writeValueAsString(obj);
            // json转Map
            params = mapper.readValue(json, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return params;
   }-->

}
